package com.rayson.oneapi.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rayson.oneapi.annotation.AuthCheck;
import com.rayson.oneapi.common.R;
import com.rayson.oneapi.model.entity.InterfaceInfo;
import com.rayson.oneapi.model.req.IdRequest;
import com.rayson.oneapi.model.req.interfaceinfo.InterfaceInfoAddReq;
import com.rayson.oneapi.model.req.interfaceinfo.InterfaceInfoListReq;
import com.rayson.oneapi.model.req.interfaceinfo.InterfaceInfoUpdateReq;
import com.rayson.oneapi.service.InterfaceInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * Author: rayson
 * Description:
 * Version: 1.0
 * Date: 2024-08-21 22:14
 */
@RestController
@RequestMapping("/interface")
@Api(tags = "接口信息管理")
public class InterfaceInfoController {

    @Resource
    private InterfaceInfoService interfaceInfoService;


    @GetMapping("/list")
    @ApiOperation("获取接口信息列表")
    public R<Page<InterfaceInfo>> list(InterfaceInfoListReq listReq) {
        Page<InterfaceInfo> page = interfaceInfoService.queryPage(listReq);
        return R.ok(page);
    }

    @PostMapping("/")
    @ApiOperation("新增接口信息")
    public R<Void> add(@RequestBody @Valid InterfaceInfoAddReq addReq) {
        interfaceInfoService.add(addReq);
        return R.ok(null, "新增接口成功");
    }

    @PutMapping("/")
    @ApiOperation("更新接口信息")
    public R<Void> update(@RequestBody @Valid InterfaceInfoUpdateReq updateReq) {
        interfaceInfoService.updateInterfaceInfo(updateReq);
        return R.ok(null, "更新接口成功");
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除接口信息")
    public R<Void> delete(@PathVariable String id) {
        interfaceInfoService.deleteInterfaceInfo(id);
        return R.ok(null, "删除接口成功");
    }

    @PutMapping("/approve")
    @ApiOperation("审核接口")
    @AuthCheck(mustRole = "admin")
    public R<Void> online(@RequestBody @Valid IdRequest idReq) {
        interfaceInfoService.onlineInterfaceInfo(idReq);
        return R.ok(null, "接口已上线");
    }

    @PutMapping("/offline")
    @ApiOperation("下线接口")
    @AuthCheck(mustRole = "admin")
    public R<Void> offlineline(@RequestBody @Valid IdRequest idReq) {
        interfaceInfoService.offlineInterfaceInfo(idReq);
        return R.ok(null, "接口已下线");
    }
}
